package com.newings.bitmapobject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class DoctorView extends View {

    Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    //绘制的bitmap
    Rect srcRect = new Rect();

    //绘制的画布中bitmap的位置
    Rect dstRect = new Rect();

    @SuppressLint("UseCompatLoadingForDrawables")
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_module_point);

    @SuppressLint("UseCompatLoadingForDrawables")
    Bitmap bitmapBackground = BitmapFactory.decodeResource(getResources(), R.drawable.icon_module_yuanyuan);

    int ws ,hs;

    List<PointBean> list = new ArrayList<>();

    public DoctorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(ws,hs);
    }

    public DoctorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(ws,hs);
    }

    public DoctorView(Context context) {
        super(context);
        initView(ws,hs);
    }

    public void initView(int ws,int hs) {
        int bWidth = bitmap.getWidth();
        int bHeight = bitmap.getHeight();

        //550
        System.out.println("标记图片宽度-->" + bWidth);// it return value correctly
        System.out.println("标记图片高度-->" + bHeight);//it return value correctly

        //TODO 暂时宽高固定为屏幕 1*1 （宽高100份中的一份）
        srcRect.set(0, 0, bWidth, bHeight);

        /**
         * 确定drawable图片的位置
         * 修改规则
         * left:    距离屏幕左上角0点向X轴右侧延伸距离
         * top:     距离屏幕左上角0点向Y轴下方延伸距离
         * right:   图片右侧到Y轴的距离
         * bottom:  图片低侧到X轴的距离
         *
         * left / right  是bitmap左上角的坐标点
         * right / bottom 是bitmap右下角的坐标点
         * 位置设置发生变化，图片只正方形时，会被延伸变形
         * 相反，图片可以按照合适比例进行展示，避免延伸变形
         */
        int width = 108;
        int height = 192;
        System.out.println("背景图片宽度-->" + width);// it return value correctly
        System.out.println("背景图片高度-->" + height);//it return value correctly

        //按照项目需求，宽高按比例分配100格子
        //在横向100
        //在竖向100
        //比如需求定位： 宽度第2，高度第43
        dstRect.set(width * ws, height * hs, width * ws + bWidth, height * hs + bHeight);

        list.clear();
        list.add(new PointBean(R.drawable.icon_module_point,1,1));
        list.add(new PointBean(R.drawable.icon_module_point,1,3));
        list.add(new PointBean(R.drawable.icon_module_point,2,2));
        list.add(new PointBean(R.drawable.icon_module_point,3,4));

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for(PointBean pointBean : list){
            toDraw(canvas,pointBean.drawRes,pointBean.ws,pointBean.hs);
        }
    }

    public void toDraw(Canvas canvas,int drawRes,int ws,int hs){
        @SuppressLint("UseCompatLoadingForDrawables")
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawRes);
        int bWidth = bitmap.getWidth();
        int bHeight = bitmap.getHeight();
        srcRect.set(0, 0, bWidth, bHeight);
        int width = 108;
        int height = 192;
        dstRect.set(width * ws, height * hs, width * ws + bWidth, height * hs + bHeight);
        canvas.drawBitmap(bitmap, srcRect, dstRect, mPaint);
        bitmap = null;
    }

    /**
     * 改变point点的绘制位置
     */
    public void setmPaintLocation(int ws,int hs){
        /**
         * //只会触发执行onDraw方法，只会改变绘制里面的内容,条目的绘制
         * invalidate();
         *
         *  //只会触发执行onDraw方法，但是可以在子线程中刷新
         * postInvalidate();
         *
         *  //view的布局参数改变之后刷新，比如view的宽度和高度都修改了，只能通过requestLayout()方法刷新
         * requestLayout();
         */
        initView(ws,hs);
        invalidate();
    }

    /**
     * 更换图片
     * @param drawable
     */
    public void setDrawableMethod(int drawable){
        this.bitmap = BitmapFactory.decodeResource(getResources(), drawable);
        invalidate();
    }

    /**
     * 批量添加标识图标
     */
    public void setmPaintList(List<PointBean> list){
        this.list.clear();
        this.list.addAll(list);
        invalidate();
    }
}
